/**
 * Created by ZT on 2018/7/5 0005.
 * 该js 存放所有的sql语句
 *
 */
//分页查找数据 [表，当前页，每页行数]
let findPageData=(table,page,per_page)=>{
    let _sql =`select * from ${table} limit ${(page-1)*per_page},${per_page}`;
    return _sql;
};
/*查找所有数据*/
let findData =(table,union)=>{
    if(union!=''){
    var _sql = `select * from ${table} ${union}`;
    }else{
        var _sql = `select * from ${table}`;
    }
    return _sql;
};
/*
*连表查询数据
*
* */
let coTableData=(table,union,fields,page=1,per_page=20)=>{
    var startLimit = (page-1)*per_page;
    var limit =`${startLimit},${per_page}`;
    var _sql =`select ${fields} from ${table} A ${union} Limit ${limit}`;
    return _sql;

}
/*
* 计算数据长度
* */
let countTableLen = (table,condition)=>{
   var _sql =`select count(1) as len from ${table} ${condition}`;
   return _sql;

};
/*
* 通过id去查询数据
* */
let fidDateFromTable =(table,id,union,fields)=>{
     var _sql =`select ${fields} from ${table} A ${union}  order by A.create_time desc`;
     return _sql;
};

/*插入数据库 */
let insertUnionTable =(table,fields,value)=>{

};
/*查询最后一条数据id*/
let findLatId =(table)=>{
    var _sql =`select LAST_INSERT_ID() from ${table}`;
    return _sql;
};
let insertTable=(table,fields,value)=>{
    var _sql =`insert into ${table} (${fields}) values (${value})`;
    return _sql;
};
/*删除数据*/
let DeleteData=(table,where)=>{
    var _sql =`delete from ${table} where ${where}`;
    return _sql;
};
/*某个字段的总和*/
let coTotal =(table)=>{
    var _sql =`select sum(count) as count from ${table}`;
    return _sql;
};
let update=(table,union)=>{
    var _sql =`update ${table} ${union}`;
    return _sql;
}

module.exports  = {
    findData,
    findPageData,
    coTableData,
    countTableLen,
    fidDateFromTable,
    insertUnionTable,
    insertTable,
    findLatId,
    DeleteData,
    coTotal,
    update
}